<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Think;
use Think\Auth;
class IndexController extends Controller {
    /**
     * 登录
     * @author 杨智超 562608962@QQ.com
     */
    public function login(){
        if ($_POST){
            $where["username"]=$_POST["username"];
            $where["status"]=1;
            $user=M("users")->where($where)->find();
            if($user){
                if($user["password"]==md5($_POST["password"])){
                    session_start();
                    $_SESSION["id"]=$user["id"];
                    $date["logintime"]=date('Y-m-d H:i:s');
                    $date["ip"]=get_client_ip();
                    $login=M("users")->where("id=".$user["id"])->save($date);
                    $data["uid"]=$_SESSION["id"];
                    $data["content"]="登录成功";
                    $data["time"]=date("Y-m-d H:i:s");
                    $data["ip"]=get_client_ip();
                    //M("data")->add($data);
                    $this->success("登录成功",U("index"));
                }else{
                    $this->error("密码错误",U("login"));
                }exit;
            }else {
                $this->error("没有此用户",U("login"));
            }exit;
        }
        $this->display("login");
    }
    /**
     * 主页
     * @author 杨智超 562608962@QQ.com
     */
    public function index(){
        $where["id"]=$_SESSION["id"];
        $user=M("users")->where($where)->find();
        //$megnum=M("message")->where("'read' = 0")->count();
        if($megnum==0){
            $megnum=Null;
        }
        $auth=new Auth();
        $group=$auth->getGroups($_SESSION["id"]);
        $user["group"]=$group[0]["title"];
        $allmenu=M("auth_rule")->where("menu = 1")->select();
        $i=0;
        foreach ($allmenu as $key => $value){
            if($auth->check($value["name"],$_SESSION["id"])){
                $menu[$i]=$value;
                $i++;
            }
        }
        $config=M("config")->find();  
        $this->assign("config",$config);
        $this->assign("menu",$menu);
        $this->assign("megnum",$megnum);
        $this->assign("user",$user);
        $this->display("index");
    }
    /**
     * 欢迎
     * @author 杨智超 562608962@QQ.com
     */
    public function welcome(){
        $todaysta=mktime(0,0,0,date(m),date(d),date(Y));
        $todaysta=date('Y-m-d H:i:s',$todaysta);
        $where["id"]=$_SESSION["id"];
        $group["group_id"]=1;
        $user=M("users")->where($where)->find();
        $today["CreateDate"]=array('between',$todaysta.",".date('Y-m-d H:i:s'));
        $today["logintime"]=array('between',$todaysta.",".date('Y-m-d H:i:s'));
        $count["subcount"]=M("subject")->count();
        
        $count["rescount"]=M("resource")->count();
        $count["usecount"]=M("users")->count();
        $count["admcount"]=M("auth_group_access")->where($group)->count();

        $count["tosubcount"]=M("subject")->where($today)->count();
       
        $count["torescount"]=M("resource")->where($today)->count();
        $count["tousecount"]=M("users")->where($today)->count();
        $count["toadmcount"]=M("auth_group_access")->where($group)->where($today)->count();

        $yestodaysta =  strtotime(date('Y-m-d').' 0:0:0');
        $yestodaysta=date('Y-m-d H:i:s',strtotime('-1 day',$yestodaysta));
        $yestodayend =  strtotime(date('Y-m-d H:i:s'));
        $yestodayend=date('Y-m-d H:i:s',strtotime('-1 day',$yestodayend));
        $yestoday["CreateDate"]=array('between',$yestodaysta.",".$yestodayend);
        $yestoday["logintime"]=array('between',$yestodaysta.",".$yestodayend);
        $count["yesubcount"]=M("subject")->where($yestoday)->count();
        
        $count["yerescount"]=M("resource")->where($yestoday)->count();
        $count["yeusecount"]=M("users")->where($yestoday)->count();
        $count["yeadmcount"]=M("auth_group_access")->where($yestoday and $group)->count();

        $weeksta =  strtotime(date('Y-m-d').' 0:0:0');
        $weeksta=date('Y-m-d H:i:s',strtotime('-1 week',$weeksta));
        $week["CreateDate"]=array('between',$weeksta.",".date('Y-m-d H:i:s'));
        $week["logintime"]=array('between',$weeksta.",".date('Y-m-d H:i:s'));
        $count["wksubcount"]=M("subject")->where($week)->count();
       
        $count["wkrescount"]=M("resource")->where($week)->count();
        $count["wkusecount"]=M("users")->where($week)->count();
        $count["wkadmcount"]=M("auth_group_access")->where($group)->where($week)->count();

        $infomation["computername"]=gethostbyaddr("127.0.0.1");
        $infomation["ip"]=get_client_ip();
        $infomation["server"]=$_SERVER['HTTP_HOST'];
        $infomation["dk"]=$_SERVER['SERVER_PORT'];
        $infomation["os"]=$_SERVER['SERVER_SOFTWARE'];
        $infomation["url"]=$_SERVER['CONTEXT_DOCUMENT_ROOT'];
        $infomation["language"]=$_SERVER['HTTP_ACCEPT_LANGUAGE'];
        $infomation["new"]=date("Y-m-d H:i:s");
        $infomation["llq"]=$_SERVER['HTTP_USER_AGENT'];
        $infomation["time"]=$_SERVER['REQUEST_TIME'];
        $infomation["ljyq"]=$_SERVER['SERVER_SOFTWARE'];
        $infomation["cpunum"]=$_SERVER['PROCESSOR_IDENTIFIER'];
        $infomation["sync"]=memory_get_usage();
        $infomation["sesnum"]=count($_SESSION);
        $config=M("config")->find();  
        $this->assign("config",$config);
        $this->assign("infomation",$infomation);
        $this->assign("count",$count);
        $this->assign("user",$user);
        $this->display("welcome");
    }
    /**
     * 退出
     * @author 杨智超 562608962@QQ.com
     */
    public function setout(){
        $_SESSION=null;
        $this->display("login");
    }
      
}